home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: Bradd W. Szonye <bradds@ix.netcom.com>
- Newsgroups: comp.lang.c++
- Subject: RE: Printing fixed length char arrays in C++?
- Date: 20 Apr 1996 16:58:52 GMT
- Organization: Netcom
- Message-ID: <01bb2edb.180de560$65c2b7c7@Zany.localhost>
- References: <Pine.SGI.3.91.960418165036.21879A-100000@golgi> <4l83t7$l0@newsbf02.news.aol.com>
- NNTP-Posting-Host: det-mi3-05.ix.netcom.com
- X-NETCOM-Date: Sat Apr 20 11:58:52 AM CDT 1996
- X-Newsreader: Microsoft Internet News
-
-
- On Friday, April 19, 1996, Blumenberg wrote...
- > Thanks for the reply. Unfortunately the fixed length char arrays are in
- > structures in system-supplied C header files. Converting them to C++
- > classes isn't worth the effort and would be a maintenance nightmare.
- > Thanks,
- > Dwayne
- >
- Another solution would be to write a custom iostream manipulator.
- How to do this depends a little bit on how your compiler implements
- manipulators with parameters, but with a little research or browsing
- through your system headers you should be able to figure it out.
-
- The syntax you would want is something like this:
- char s[10] = . . .
- cout << fixwidth(s, sizeof s) << endl;
-
- and you'd write something like this:
- ostream& fixwidth(ostream& o, const char* s, size_t len)
- {
- for (size_t i = 0; i < len; i++) {
- if (s[i] == '\0') break; // optional
- o << s[i];
- }
- }
-
- The optional line stops the write if it finds a null terminator inside the
- fixed-width string. Leave it in and the manipulator works like "strnxxx"
- functions, take it out and it works like "memxxx" functions.
-
- The only thing left is how to hook the custom fixwidth into the iostreams
- system. How to do this is generally dependent on which compiler you use.
-
-
-